Refactor OmniAuth integration.

- Make use of Devise's OmniAuth integration.
- Merge config/initializers/omniauth.rb into config/initializers/devise.rb.
- Define button labels in config/locales/devise.en.yml.
- Fix devise's routes so we can use user_omniauth_authorize_path().

Akinori MUSHA 10 years ago
parent
commit
87ad26301b

+ 2 - 2
app/concerns/twitter_concern.rb

@@ -5,9 +5,9 @@ module TwitterConcern
5 5
     include Oauthable
6 6
 
7 7
     validate :validate_twitter_options
8
-    valid_oauth_providers 'twitter'
8
+    valid_oauth_providers :twitter
9 9
 
10
-    gem_dependency_check { defined?(Twitter) && has_oauth_configuration_for?('twitter') }
10
+    gem_dependency_check { defined?(Twitter) && Devise.omniauth_providers.include?(:twitter) }
11 11
   end
12 12
 
13 13
   def validate_twitter_options

+ 9 - 0
app/helpers/application_helper.rb

@@ -40,4 +40,13 @@ module ApplicationHelper
40 40
       link_to 'No', agent_path(agent, tab: (agent.recent_error_logs? ? 'logs' : 'details')), class: 'label label-danger'
41 41
     end
42 42
   end
43
+
44
+  def icon_for_service(service)
45
+    case service.to_sym
46
+    when :twitter, :github
47
+      "<i class='fa fa-#{service}'></i>".html_safe
48
+    else
49
+      "<i class='fa fa-lock'></i>".html_safe
50
+    end
51
+  end
43 52
 end

+ 1 - 1
app/models/agents/basecamp_agent.rb

@@ -3,7 +3,7 @@ module Agents
3 3
     cannot_receive_events!
4 4
 
5 5
     include Oauthable
6
-    valid_oauth_providers '37signals'
6
+    valid_oauth_providers :'37signals'
7 7
 
8 8
     description <<-MD
9 9
       The BasecampAgent checks a Basecamp project for new Events

+ 4 - 4
app/models/service.rb

@@ -64,10 +64,10 @@ class Service < ActiveRecord::Base
64 64
   end
65 65
 
66 66
   def self.provider_specific_options(omniauth)
67
-    case omniauth['provider']
68
-      when 'twitter', 'github'
67
+    case omniauth['provider'].to_sym
68
+      when :twitter, :github
69 69
         { name: omniauth['info']['nickname'] }
70
-      when '37signals'
70
+      when :'37signals'
71 71
         { user_id: omniauth['extra']['accounts'][0]['id'], name: omniauth['info']['name'] }
72 72
       else
73 73
         { name: omniauth['info']['nickname'] }
@@ -86,4 +86,4 @@ class Service < ActiveRecord::Base
86 86
                                 options: options
87 87
     end
88 88
   end
89
-end
89
+end

+ 2 - 4
app/models/user.rb

@@ -1,10 +1,8 @@
1 1
 # Huginn is designed to be a multi-User system.  Users have many Agents (and Events created by those Agents).
2 2
 class User < ActiveRecord::Base
3
-  # Include default devise modules. Others available are:
4
-  # :token_authenticatable, :confirmable,
5
-  # :lockable, :timeoutable and :omniauthable
6 3
   devise :database_authenticatable, :registerable,
7
-         :recoverable, :rememberable, :trackable, :validatable, :lockable
4
+         :recoverable, :rememberable, :trackable, :validatable, :lockable,
5
+         :omniauthable
8 6
 
9 7
   INVITATION_CODES = [ENV['INVITATION_CODE'] || 'try-huginn']
10 8
 

+ 3 - 9
app/views/services/index.html.erb

@@ -11,15 +11,9 @@
11 11
         <%= link_to 'wiki', 'https://github.com/cantino/huginn/wiki/Configuring-OAuth-applications', target: :_blank %>
12 12
         for guidance.
13 13
       </p>
14
-      <% if has_oauth_configuration_for?('twitter') %>
15
-        <p><%= link_to "/auth/twitter", class: 'btn btn-default btn-auth btn-auth-twitter' do %><i class='fa fa-twitter'></i><span>Authenticate with Twitter</span><% end %></p>
16
-      <% end %>
17
-      <% if has_oauth_configuration_for?('37signals') %>
18
-        <p><%= link_to "/auth/37signals", class: 'btn btn-default btn-auth btn-auth-37signals' do %><i class='fa fa-lock'></i><span>Authenticate with 37Signals (Basecamp)</span><% end %></p>
19
-      <% end -%>
20
-      <% if has_oauth_configuration_for?('github') %>
21
-        <p><%= link_to "/auth/github", class: 'btn btn-default btn-auth btn-auth-github' do %><i class='fa fa-github'></i><span>Authenticate with Github</span><% end %></p>
22
-      <% end -%>
14
+      <%- Devise.omniauth_providers.each { |provider| -%>
15
+        <p><%= link_to user_omniauth_authorize_path(provider), class: "btn btn-default btn-auth btn-auth-#{provider}" do %><%= icon_for_service(provider) %><span>Authenticate with <%= t("devise.omniauth_providers.#{provider}") %></span><% end %></p>
16
+      <%- } -%>
23 17
       <hr>
24 18
 
25 19
       <div class='table-responsive'>

+ 19 - 1
config/initializers/devise.rb

@@ -213,6 +213,23 @@ Devise.setup do |config|
213 213
   # Add a new OmniAuth provider. Check the wiki for more information on setting
214 214
   # up on your models and hooks.
215 215
   # config.omniauth :github, 'APP_ID', 'APP_SECRET', :scope => 'user,public_repo'
216
+  if defined?(OmniAuth::Strategies::Twitter) &&
217
+     (key = ENV["TWITTER_OAUTH_KEY"]).present? &&
218
+     (secret = ENV["TWITTER_OAUTH_SECRET"]).present?
219
+    config.omniauth :twitter, key, secret, authorize_params: {force_login: 'true', use_authorize: 'true'}
220
+  end
221
+
222
+  if defined?(OmniAuth::Strategies::ThirtySevenSignals) &&
223
+     (key = ENV["THIRTY_SEVEN_SIGNALS_OAUTH_KEY"]).present? &&
224
+     (secret = ENV["THIRTY_SEVEN_SIGNALS_OAUTH_SECRET"]).present?
225
+    config.omniauth :'37signals', key, secret
226
+  end
227
+
228
+  if defined?(OmniAuth::Strategies::GitHub) &&
229
+     (key = ENV["GITHUB_OAUTH_KEY"]).present? &&
230
+     (secret = ENV["GITHUB_OAUTH_SECRET"]).present?
231
+    config.omniauth :github, key, secret
232
+  end
216 233
 
217 234
   # ==> Warden configuration
218 235
   # If you want to use other strategies, that are not supported by Devise, or
@@ -236,4 +253,5 @@ Devise.setup do |config|
236 253
   # When using omniauth, Devise cannot automatically set Omniauth path,
237 254
   # so you need to do it manually. For the users scope, it would be:
238 255
   # config.omniauth_path_prefix = "/my_engine/users/auth"
239
-end
256
+  config.omniauth_path_prefix = "/auth"
257
+end

+ 0 - 35
config/initializers/omniauth.rb

@@ -1,35 +0,0 @@
1
-OMNIAUTH_PROVIDERS = {}.tap { |providers|
2
-  if defined?(OmniAuth::Strategies::Twitter) &&
3
-     (key = ENV["TWITTER_OAUTH_KEY"]).present? &&
4
-     (secret = ENV["TWITTER_OAUTH_SECRET"]).present?
5
-    providers['twitter'] = {
6
-      omniauth_params: [key, secret, authorize_params: {force_login: 'true', use_authorize: 'true'}]
7
-    }
8
-  end
9
-
10
-  if defined?(OmniAuth::Strategies::ThirtySevenSignals) &&
11
-     (key = ENV["THIRTY_SEVEN_SIGNALS_OAUTH_KEY"]).present? &&
12
-     (secret = ENV["THIRTY_SEVEN_SIGNALS_OAUTH_SECRET"]).present?
13
-    providers['37signals'] = {
14
-      omniauth_params: [key, secret]
15
-    }
16
-  end
17
-
18
-  if defined?(OmniAuth::Strategies::GitHub) &&
19
-     (key = ENV["GITHUB_OAUTH_KEY"]).present? &&
20
-     (secret = ENV["GITHUB_OAUTH_SECRET"]).present?
21
-    providers['github'] = {
22
-      omniauth_params: [key, secret]
23
-    }
24
-  end
25
-}
26
-
27
-def has_oauth_configuration_for?(provider)
28
-  OMNIAUTH_PROVIDERS.key?(provider.to_s)
29
-end
30
-
31
-Rails.application.config.middleware.use OmniAuth::Builder do
32
-  OMNIAUTH_PROVIDERS.each { |name, config|
33
-    provider name, *config[:omniauth_params]
34
-  }
35
-end

+ 4 - 0
config/locales/devise.en.yml

@@ -49,6 +49,10 @@ en:
49 49
     omniauth_callbacks:
50 50
       success: 'Successfully authenticated from %{kind} account.'
51 51
       failure: 'Could not authenticate you from %{kind} because "%{reason}".'
52
+    omniauth_providers:
53
+      twitter: 'Twitter'
54
+      github: 'GitHub'
55
+      37signals: '37Signals (Basecamp)'
52 56
     mailer:
53 57
       confirmation_instructions:
54 58
         subject: 'Confirmation instructions'

+ 2 - 1
config/routes.rb

@@ -66,8 +66,9 @@ Huginn::Application.routes.draw do
66 66
   post  "/users/:user_id/webhooks/:agent_id/:secret" => "web_requests#handle_request" # legacy
67 67
   post  "/users/:user_id/update_location/:secret" => "web_requests#update_location" # legacy
68 68
 
69
+  match '/auth/:provider/callback', to: 'services#callback',
70
+        via: [:get, :post], constraints: { provider: Regexp.union(Devise.omniauth_providers.map(&:to_s)) }
69 71
   devise_for :users, :sign_out_via => [ :post, :delete ]
70
-  get '/auth/:provider/callback', to: 'services#callback'
71 72
 
72 73
   get "/about" => "home#about"
73 74
   root :to => "home#index"